package consultas;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import json.JSONException;
import json.JSONObject;

public class Consulta {
	String direccion;
	String parametro_consulta;
	
	public Consulta(String d)
	{
		direccion=d;
	}
	public Consulta(String d,String p)
	{
		direccion=d;
		parametro_consulta=p;
	}
	public void insercion()
	{
		URL yahoo;
		try {

			yahoo = new URL(direccion);

			URLConnection yc = yahoo.openConnection();

			BufferedReader in= new BufferedReader(new InputStreamReader(yc.getInputStream()));

			String inputLine;
			inputLine = in.readLine();
			in.close();
			
		} catch (MalformedURLException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} catch (IOException e1) {

			// TODO Auto-generated catch block

			e1.printStackTrace();

		}
	}
	
	
	public String consultaId()
	{
		URL urlSM;
		String idSM=null;
		try {
			urlSM = new URL(direccion);

			BufferedReader readerSM;
			try {
				readerSM = new BufferedReader(new InputStreamReader(urlSM.openStream()));
				String lineSM;
				lineSM = readerSM.readLine();
				readerSM.close();

				try {
						JSONObject joSM = new JSONObject(lineSM);
						idSM = joSM.getString(parametro_consulta);
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (MalformedURLException e2) 
		{
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		return idSM;
	}
	
	public String consulta()
	{
		URL urlSM;
		String lineSM=null;
		String idSM=null;
		try {
			urlSM = new URL(direccion);

			BufferedReader readerSM;
			try {
				readerSM = new BufferedReader(new InputStreamReader(urlSM.openStream()));
				lineSM = readerSM.readLine();
				readerSM.close();
				return lineSM;
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (MalformedURLException e2) 
		{
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		return lineSM;
	}
		
}
